<template>
  <div class="login-container">
    <div class="login-box">
      <!-- 头像 -->
      <div class="avatar-box">
        <img src="../assets/logo.png" alt="" />
      </div>
      <!-- 登录表单 -->
      <el-form
        ref="LoginFormRef"
        :model="loginForm"
        :rules="LoginFormRules"
        class="login-form"
      >
        <!-- 账号 -->
        <el-form-item prop="name">
          <el-input
            prefix-icon="iconfont icon-dengluyemianyonghuming"
            v-model="loginForm.name"
          ></el-input>
        </el-form-item>
        <!-- 密码-->
        <el-form-item prop="password">
          <el-input
            prefix-icon="iconfont icon-dengluyemianmima"
            v-model="loginForm.password"
            type="password"
          ></el-input>
        </el-form-item>
        <!-- 操作 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        name: 'admin',
        password: '123456',
      },
      LoginFormRules: {
        name: [
          {
            required: true,
            message: '请输入用户名称',
            trigger: 'blur',
          },
          {
            min: 3,
            max: 5,
            message: '长度在 3 到 5 个字符',
            trigger: 'blur',
          },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 6,
            max: 15,
            message: '长度在 6 到 15 个字符',
            trigger: 'blur',
          },
        ],
      },
    }
  },
  methods: {
    // 重置按钮
    resetLoginForm() {
      this.$refs.LoginFormRef.resetFields()
    },
    // 登录按钮
    login() {
      // 表单预校验
      this.$refs.LoginFormRef.validate(async (valid) => {
        console.log(valid)
        // 校验失败
        if (!valid) return
        // 验证成功
        //  以下内容 接后有误  重要！！！！
        // const { data: res } = await this.$http.post('login', this.loginForm)
        // if (res.meta.status !== 200) return this.message.error('登录失败')
        // this.message.success('登录成功') // 登录成功后将toke保存到客户端的sessionStorage中,项目出了登录之外的其他的Api接口必须在登录之后才能访问，token 旨在当前网站打开期间生效，所以将token保存在sessionStorge中
        // window.sessionStorage.setItem('token', res.data.token)
        // 通过编程式导航跳转到后台主页
        this.router.push('/home')
      })
    },
  },
}
</script>

<style scope>
.login-container {
  background-color: #2b4b6b;
  height: 100%;
}
.login-box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.avatar-box {
  height: 130px;
  width: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
}
.avatar-box img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background-color: #eee;
}
.login-form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
.btns {
  display: flex;
  justify-content: flex-end;
}
</style>
